package com.vito.oa.email.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.DataGrid;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.oa.email.model.ExternalContacts;
import com.vito.oa.email.model.ExternalContactsGroup;
import com.vito.oa.email.model.OaEmailContant;
import com.vito.oa.email.service.ExternalContactsGroupService;
import com.vito.oa.email.service.ExternalContactsService;
import com.vito.oa.email.service.OaBaseUserService;

@Controller
@RequestMapping("oa/emailContacts")
public class OaEmailContactsAction extends BaseAction{
	
	private static final Logger log = LoggerFactory.getLogger(OaEmailContactsAction.class);
	
	@Reference
	private ExternalContactsService contactsService;
	@Reference
	private ExternalContactsGroupService contactsGroupService;
	@Reference
	private OaBaseUserService oaBaseUserService;
	
	/**
	 * 外部联系人分页列表 
	 * @param contacts
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getContactsPage")
	public Result getContactsPage(ExternalContacts contacts) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			contacts.setStatus(OaEmailContant.STATUS_NORMAL);
			contacts.setUserId(user.getUserId());
			DataGrid<ExternalContacts> list = contactsService.getContactsByPage(contacts);
			if (list != null && list.getTotal() > 0) {
				rs.setCode(Result.CODE_OK);
				rs.setData(list);
				return rs;
			} else {
				rs.setCode(Result.CODE_NO_DATA);
				rs.setMsg("没有数据");
				return rs;
			}
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 添加联系人
	 * @param contacts
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addContacts")
	public Result addContacts(ExternalContacts contacts) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if (Validata.isNullOrEmpty(contacts.getContactsName())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("姓名不能为空");
				return rs;
			}
			if (Validata.isNullOrEmpty(contacts.getContactsMail())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("邮箱不能为空");
				return rs;
			}
			if (Validata.isNullOrEmpty(contacts.getGroupId())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("请选择分组");
				return rs;
			}
			boolean b = false;
			contacts.setUserId(user.getUserId());
			b = contactsService.addContacts(contacts);
			if (b) {
				rs.setCode(Result.CODE_OK);
				rs.setMsg("保存成功");
			} else {
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("保存失败");
			}
			return rs;
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 删除联系人
	 * @param contactsId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delContacts")
	public Result delContacts(String contactsIds) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if (Validata.isNullOrEmpty(contactsIds)) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("联系人Id不能为空");
				return rs;
			}
			boolean b = contactsService.delContacts(contactsIds, OaEmailContant.STATUS_DEL);
			if (b) {
				rs.setCode(Result.CODE_OK);
				rs.setMsg("删除成功");
			} else {
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("删除失败");
			}
			return rs;
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 修改联系人
	 * @param contacts
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateContacts")
	public Result updateContacts(ExternalContacts contacts) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if (Validata.isNullOrEmpty(contacts.getContactsId())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("联系人Id不能为空");
				return rs;
			}
			if (Validata.isNullOrEmpty(contacts.getGroupId())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("请选择分组");
				return rs;
			}
			boolean b = contactsService.updateContacts(contacts);
			if (b) {
				rs.setCode(Result.CODE_OK);
				rs.setMsg("修改成功");
			} else {
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("修改失败");
			}
			return rs;
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 添加分组
	 * @param contactsGroup
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/addContactsGroup")
	public Result addContactsGroup(ExternalContactsGroup contactsGroup) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if (Validata.isNullOrEmpty(contactsGroup.getGroupName())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("组名不能为空");
				return rs;
			}
			contactsGroup.setUserId(user.getUserId());
			boolean b = contactsGroupService.addContactsGroup(contactsGroup);
			if (b) {
				rs.setCode(Result.CODE_OK);
				rs.setMsg("保存成功");
			} else {
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("保存失败");
			}
			return rs;
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 修改分组
	 * @param contactsGroup
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateContactsGroup")
	public Result updateContactsGroup(ExternalContactsGroup contactsGroup) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if (Validata.isNullOrEmpty(contactsGroup.getGroupName())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("组名不能为空");
				return rs;
			}
			if (Validata.isNullOrEmpty(contactsGroup.getGroupId())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("组id不能为空");
				return rs;
			}
			boolean b = contactsGroupService.updateContactsGroup(contactsGroup);
			if (b) {
				rs.setCode(Result.CODE_OK);
				rs.setMsg("保存成功");
			} else {
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("保存失败");
			}
			return rs;
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 检查组名重复接口
	 * @param contactsGroup
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/checkGroupName")
	public Result checkGroupName(String groupName) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if (Validata.isNullOrEmpty(groupName)) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("组名不能为空");
				return rs;
			}
			ExternalContactsGroup group = new ExternalContactsGroup();
			group.setUserId(user.getUserId());
			group.setGroupName(groupName);
			ExternalContactsGroup contactsGroup = contactsGroupService.getGroupByName(group);
			if (contactsGroup == null) {
				rs.setCode(Result.CODE_OK);
				rs.setMsg("组名可用");
				return rs;
			} else {
				rs.setCode(Result.CODE_INTER_ERR);
				rs.setMsg("组名已存在");
				return rs;
			}
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 删除分组
	 * @param contactsGroup
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delContactsGroup")
	public Result delContactsGroup(ExternalContactsGroup contactsGroup) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if (Validata.isNullOrEmpty(contactsGroup.getGroupId())) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("组id不能为空");
				return rs;
			}
			List<ExternalContacts> list = contactsService.getContactsByGroupId(contactsGroup.getGroupId());
			boolean b = false;
			ExternalContacts whereContacts = new ExternalContacts();
			whereContacts.setStatus(OaEmailContant.STATUS_DEL);
			for (ExternalContacts contacts : list) {
				whereContacts.setContactsId(contacts.getContactsId());
				//删除组内成员
				b = contactsService.updateContacts(whereContacts);
			}
			//处理组
			contactsGroup.setStatus(OaEmailContant.STATUS_DEL);
			b = contactsGroupService.updateContactsGroup(contactsGroup);
			if (b) {
				rs.setCode(Result.CODE_OK);
				rs.setMsg("删除成功");
			} else {
				log.error("删除联系人组失败");
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg("删除失败");
			}
			return rs;
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 分组列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getContactsGroupList")
	public Result getContactsGroupList(){
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			List<ExternalContactsGroup> list = contactsGroupService.getGroupByUserId(user.getUserId());
			rs.setCode(Result.CODE_OK);
			rs.setData(list);
			return rs;
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 分组列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getContactsGroupTree")
	public Result getContactsGroupTree(){
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			List<ExternalContactsGroup> list = contactsGroupService.getGroupTreeByUserId(user.getUserId());
			rs.setCode(Result.CODE_OK);
			rs.setData(list);
			return rs;
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 检查邮箱
	 * @param contactsMail
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/checkContactsMail")
	public Result checkContactsMail(String contactsMail) {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			if (Validata.isNullOrEmpty(contactsMail)) {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("邮箱不能为空");
				return rs;
			}
			ExternalContacts contacts = new ExternalContacts();
			contacts.setContactsMail(contactsMail);
			contacts.setUserId(user.getUserId());
			ExternalContacts externalContacts = contactsService.getContactsByMail(contacts);
			if (externalContacts == null) {
				rs.setCode(Result.CODE_OK);
			} else {
				rs.setCode(Result.CODE_PARA_ERROR);
				rs.setMsg("邮箱已存在");
			}
			return rs;
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 查询外部联系人树
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getEmailContactsTree")
	public Result getEmailContactsTree() {
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			List<ExternalContactsGroup> list = contactsGroupService.getEmailContactsTree(user.getUserId());
			if (list != null) {
				rs.setCode(Result.CODE_OK);
				rs.setData(list);
				return rs;
			} else {
				rs.setCode(Result.CODE_NO_DATA);
				rs.setMsg("没有数据");
				return rs;
			}
		} catch (Exception e) {
			log.error("执行异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("执行异常");
			return rs;
		}
	}
	
	/**
	 * 查询内部联系人
	 * @return
	 */
	@RequestMapping("getInnerContactsTree")
	@ResponseBody
	public Result getInnerContactsTree(){
		Result rs = new Result();
		try{
			UserModel user = SessionMap.getObj();
			if (user == null || Validata.isNullOrEmpty(user.getUserId()) || Validata.isNullOrEmpty(user.getDeptParentId())) {
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			List<Map<String,String>> userlistDeptTree = oaBaseUserService.getInnerContactsTree(user.getDeptParentId());
			if (userlistDeptTree != null && userlistDeptTree.size() > 0) {
				rs.setData(userlistDeptTree);
				rs.setCode(Result.CODE_OK);
			}else{
				rs.setCode(Result.CODE_NO_DATA);
			}
		}catch(Exception ex){
			log.error("查询异常",ex);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("查询异常");
		}
		return rs;
	}

}
